import javafx.scene.chart.PieChart;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-05-19
 * Time: 9:31
 */
public class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        // 二分的左右边界分别为 1 和 Max(piles[i])
        int left = 1;
        int right = 0;
        for (int pile: piles) {
            right = Math.max(right, pile);
        }
        while (left < right) {
            int mid = (right - left) / 2 + left;
            int time = getTime(piles, mid);
            if (time <= h){
                right = mid;
            }else{
                left = mid + 1;
            }
        }
        return left;
    }

    private int getTime(int[] piles, int speed) {
        int time = 0;
        for (int i = 0; i < piles.length; i++) {
            // 向上取整
            int cur = (piles[i] + speed - 1) / speed;
            time += cur;
        }
        return time;
    }
}
